/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#import "MessageWindowController.h"

#import "CHBrowserView.h"
#import "MessageViewContainer.h"
#import "MessageHeader.h"
#import "MessageComposeService.h"
#import "nsIMsgHdr.h"
#import "nsIMsgFolder.h"

// Toolbar Identifiers
static NSString* const kMessageWindowToolbarIdentifier = @"MessageWindowToolbarIdentifier";
static NSString* const kDeleteMailToolbarIdentifier = @"DeleteMailToolbarIdentifier";
static NSString* const kReplyToolbarIdentifier = @"ReplyToolbarIdentifier";
static NSString* const kReplyAllToolbarIdentifier = @"ReplyAllToolbarIdentifier";
static NSString* const kFowardToolbarIdentifier = @"FowardToolbarIdentifier";
static NSString* const kAddressBookToolbarIdentifier = @"AddressBookToolbarIdentifier";

@interface MessageWindowController(Private)

-(void)setupToolbar;

-(void)windowDidBecomeyMain:(id)sender;
-(void)windowDidResignMain:(id)sender;
-(void)windowWillClose:(id)sender;

@end

@implementation MessageWindowController

-(id)initWithMessageHeader:(MessageHeader*)aMessageHeader
                messageUri:(NSString*)aMessageUri
           andCharacterSet:(NSString*)aCharSet
{
  if ((self = [super initWithWindowNibName:@"MessageWindow"]))
  {
    mMessageHeader = [aMessageHeader retain];
    mMessageUri = [aMessageUri retain];
    mCharaterSet = [aCharSet retain];
  }
  
  return self;
}

-(void)dealloc
{
  [mMessageHeader release];
  [mCHBrowserView release];
  [mMessageUri release];
  [mCharaterSet release];
  
  [super dealloc];
}

-(void)awakeFromNib
{
  // Get our saved dimensions.
  //[[self window] setFrameAutosaveName:kMailWindowFrameSaveName];
  
  /* Keep this out for now...
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(windowDidBecomeMain:) 
                                               name:NSWindowDidBecomeKeyNotification
                                             object:[self window]];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(windowDidResignMain:) 
                                               name:NSWindowDidResignKeyNotification
                                             object:[self window]];
  */
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(windowWillClose:) 
                                               name:NSWindowWillCloseNotification
                                             object:[self window]];
}

-(void)windowDidLoad
{
  [super windowDidLoad];
  [self setupToolbar]; 
  
  mCHBrowserView = [[CHBrowserView alloc] initWithFrame:[mBrowserView bounds] andWindow:[self window]];
  [mBrowserView setAutoresizesSubviews:YES];
  [mBrowserView setBrowserView:mCHBrowserView];
  
  [[MessageWindowService sharedInstance] initMessageWindow:mCHBrowserView];
}

-(void)windowDidBecomeyMain:(id)sender
{
  [[MessageWindowService sharedInstance] setMessageWindowDelegate:self];
}

-(void)windowDidResignMain:(id)sender
{
  [[MessageWindowService sharedInstance] setMessageWindowDelegate:nil];
}

-(void)windowWillClose:(id)sender
{
  [[MessageWindowService sharedInstance] removeMessageWindow:self];
  [mBrowserView windowWillClose];
}

-(void)loadMessageUri
{
  [mCHBrowserView setCharset:mCharaterSet];  // retain character settings
  
  // Make sure we are the main window
  [[MessageWindowService sharedInstance] setMessageWindowDelegate:self];
  [[MessageWindowService sharedInstance] loadMessageUri:mMessageUri];
}

-(IBAction)deleteMail:(id)sender
{
  // A message can be deleted by passing an array containing the |nsIMsgHdr|
  // to the |DeleteMessages()| function of the parent |nsIMsgFolder|.
  
  //XXX write me..
  
}

-(IBAction)reply:(id)sender
{
  [[MessageComposeService sharedComposeService] replyToMessageUri:mMessageUri];
}

-(IBAction)replyAll:(id)sender
{
  [[MessageComposeService sharedComposeService] replyToAllMessageUri:mMessageUri];
}

-(IBAction)foward:(id)sender
{
  [[MessageComposeService sharedComposeService] forwardMessageUri:mMessageUri];
}

-(void)onMessageLoaded:(DummyMessageHeader*)aDummyMessageHeader
{  
  [mBrowserView updateViewForDummyHeader:aDummyMessageHeader];
  [mBrowserView showMessageHeaderView];
  [mBrowserView setBrowserViewActive:NO];
  
  [[self window] setTitle:[mMessageHeader subject]];
}

-(void)onAttachmentsLoaded:(NSArray*)aAttachmentArray
{
  if ([aAttachmentArray count] > 0)
    [mBrowserView showAttachmentView:aAttachmentArray];
  else
    [mBrowserView removeAttachmentView];
}

-(CHBrowserView*)browserMessageView
{
  return mCHBrowserView;
}

#pragma mark -

-(void)setupToolbar
{
  NSToolbar* toolbar = [[[NSToolbar alloc] initWithIdentifier:kMessageWindowToolbarIdentifier] autorelease];
  [toolbar setDelegate:self];
  [toolbar setAllowsUserCustomization:YES];
  [toolbar setAutosavesConfiguration:YES];
  [[self window] setToolbar:toolbar];
}

-(NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
  return [NSArray arrayWithObjects:
    kDeleteMailToolbarIdentifier,
    kReplyToolbarIdentifier,
    kReplyAllToolbarIdentifier,
    kFowardToolbarIdentifier,
    kAddressBookToolbarIdentifier,
    NSToolbarPrintItemIdentifier,
    NSToolbarFlexibleSpaceItemIdentifier,
    NSToolbarSpaceItemIdentifier,
    NSToolbarSeparatorItemIdentifier,
    nil];
}

-(NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
  return [NSArray arrayWithObjects:
    kDeleteMailToolbarIdentifier,
    NSToolbarSpaceItemIdentifier,
    kReplyToolbarIdentifier,
    kReplyAllToolbarIdentifier,
    kFowardToolbarIdentifier,
    NSToolbarSpaceItemIdentifier,
    NSToolbarSpaceItemIdentifier,
    NSToolbarPrintItemIdentifier,
    nil];
}

-(NSToolbarItem*) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
  NSToolbarItem *theItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
  
  if ([itemIdentifier isEqualToString:kDeleteMailToolbarIdentifier])
  {
    [theItem setLabel:NSLocalizedString(@"Delete", nil)];
    [theItem setToolTip:NSLocalizedString(@"DeleteMailToolTip", nil)];
    [theItem setPaletteLabel:NSLocalizedString(@"DeleteButton", nil)];
    [theItem setAction:@selector(deleteMail:)];
    [theItem setImage:[NSImage imageNamed:@"trash.tif"]];
  }
  else if ([itemIdentifier isEqualToString:kReplyToolbarIdentifier])
  {
    [theItem setLabel:NSLocalizedString(@"Reply", nil)];
    [theItem setToolTip:NSLocalizedString(@"ReplyToolTip", nil)];
    [theItem setPaletteLabel:NSLocalizedString(@"ReplyButton", nil)];
    [theItem setAction:@selector(reply:)];
    [theItem setImage:[NSImage imageNamed:@"circle_return.tif"]];
  }
  else if ([itemIdentifier isEqualToString:kReplyAllToolbarIdentifier])
  {
    [theItem setLabel:NSLocalizedString(@"ReplyAll", nil)];
    [theItem setToolTip:NSLocalizedString(@"ReplyAllToolTip", nil)];
    [theItem setPaletteLabel:NSLocalizedString(@"ReplyAllButton", nil)];
    [theItem setAction:@selector(replyAll:)];
    [theItem setImage:[NSImage imageNamed:@"circle_turnright.tif"]];
  }
  else if ([itemIdentifier isEqualToString:kFowardToolbarIdentifier])
  {
    [theItem setLabel:NSLocalizedString(@"Foward", nil)];
    [theItem setToolTip:NSLocalizedString(@"FowardToolTip", nil)];
    [theItem setPaletteLabel:NSLocalizedString(@"FowardButton", nil)];
    [theItem setAction:@selector(foward:)];
    [theItem setImage:[NSImage imageNamed:@"forward.tif"]];
  }
  else if ([itemIdentifier isEqualToString:kAddressBookToolbarIdentifier])
  {
    [theItem setLabel:NSLocalizedString(@"AddressBook", nil)];
    [theItem setToolTip:NSLocalizedString(@"AddressBookToolTip", nil)];
    [theItem setPaletteLabel:NSLocalizedString(@"AddressBookButton", nil)];
    [theItem setAction:@selector(openAddressBook:)];
    [theItem setImage:[NSImage imageNamed:@"address_book.tif"]];
  }
  
  // Something strange, return nil
  else
    return nil;
  
  return theItem;
}

-(BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
  if ([theItem action] == @selector(deleteMail:))
    return NO;
  
  return YES;
}


@end
